package com.zupig.colormetricdemo.View;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * Created by Leegl on 2017/5/17 0017.
 */

public class ImageHelper {

    public static Bitmap handleImageEffect(Bitmap bm,float hue, float saturation, float lum){

        Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        ///设置色调
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0,hue);
        hueMatrix.setRotate(1,hue);
        hueMatrix.setRotate(2,hue);
        //设置饱和度
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);
        //设置亮度
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum,lum,lum,1);
        //混合矩阵
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);
        //将处理效果作用到图片
        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        //android 不支持在原图上进行图片的操作，所以相当于复制了原图，在此基础上对图片进行修改
        canvas.drawBitmap(bm,0,0,paint);

        return bitmap;
    }
}
